#include "axistest.h"

axisTest::axisTest(QString titileName,QWidget *parent) : QMainWindow(parent),
    mPlot(0),
    mTag1(0),
    mTag2(0)
{
    mPlot = new QCustomPlot(this);
    setCentralWidget(mPlot);
    mPlot->plotLayout()->insertRow(0);
    m_title = new QCPTextElement(mPlot, titileName);
    mPlot->plotLayout()->addElement(0, 0, m_title);
    // configure plot to have two right axes:
    mPlot->yAxis->setTickLabels(false);//设置是否显示左侧刻度
    connect(mPlot->yAxis2, SIGNAL(rangeChanged(QCPRange)), mPlot->yAxis, SLOT(setRange(QCPRange))); // left axis only mirrors inner right axis
    mPlot->yAxis2->setVisible(true);
//    mPlot->axisRect()->addAxis(QCPAxis::atRight);
    mPlot->axisRect()->axis(QCPAxis::atRight, 0)->setPadding(50); // add some padding to have space for tags
//    mPlot->axisRect()->axis(QCPAxis::atRight, 1)->setPadding(50); // add some padding to have space for tags

    // create graphs:
    mGraph1 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 0));
//    mGraph2 = mPlot->addGraph(mPlot->xAxis, mPlot->axisRect()->axis(QCPAxis::atRight, 0));
    mGraph1->setPen(QPen(QColor(250, 120, 0)));
//    mGraph2->setPen(QPen(QColor(0, 180, 60)));
    mGraph1->setBrush(QBrush(QColor(250,120,20,70)));
//    mGraph2->setBrush(QBrush(QColor(0,180,20,70)));

    // create tags with newly introduced AxisTag class (see axistag.h/.cpp):
    mTag1 = new AxisTag(mGraph1->valueAxis());
    mTag1->setPen(mGraph1->pen());
//    mTag2 = new AxisTag(mGraph2->valueAxis());
//    mTag2->setPen(mGraph2->pen());
    timerSlot();
    connect(&mDataTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
    mDataTimer.start(100);
}



void axisTest::timerSlot()
{
  // calculate and add a new data point to each graph:

//    mGraph1->addData(mGraph1->dataCount(),staticVar);
//    mGraph1->addData(mGraph1->dataCount(), qSin(mGraph1->dataCount()/10.0)+qSin(mGraph1->dataCount()/10.0/0.3843)*0.625);
//    mGraph2->addData(mGraph2->dataCount(), qCos(mGraph2->dataCount()/50.0)+qSin(mGraph2->dataCount()/50.0/0.4364)*0.15);
//  staticVar=qSin(mGraph1->dataCount()/10.0)+qSin(mGraph1->dataCount()/10.0/0.3843)*0.625;
//  if(mGraph2->dataCount()==50)
//      staticVar= 2;

//  if(mGraph2->dataCount()==250)
//      staticVar= -3;
    addDataAndUpdateQueues(mGraph1, staticVar);

  // make key axis range scroll with the data:
  mPlot->xAxis->rescale();
  mGraph1->rescaleValueAxis(false, true);
//  mGraph2->rescaleValueAxis(false, true);
//  mPlot->xAxis->setRange(mPlot->xAxis->range().upper, 100, Qt::AlignRight);

  // 计算x轴的范围
  int currentIndex = mGraph1->dataCount() - 1; // 假设这是最新的数据点索引
  int minIndex = qMax(0, currentIndex - 99); // 确保不会小于0
  double minX = minIndex; // 通常x轴的值就是索引，除非你有不同的设置
  double maxX = currentIndex;

  // 设置x轴的范围以保持100个数据点的显示
  mPlot->xAxis->setRange(minX, maxX);

  // 计算最近一百个数据点的最大值和最小值
  double graph1Min = *std::min_element(mGraph1DataQueue.begin(), mGraph1DataQueue.end());
  double graph1Max = *std::max_element(mGraph1DataQueue.begin(), mGraph1DataQueue.end());
  if(graph1Min>0)
      graph1Min=0;
  // 对mGraph2也做同样的操作
  if(graph1Max<0)
      graph1Max=0;
  // 设置y轴的范围
  mPlot->yAxis2->setRange(graph1Min, graph1Max);

  // 对mGraph2的y轴也做同样的设置


  // update the vertical axis tag positions and texts to match the rightmost data point of the graphs:
  double graph1Value = mGraph1->dataMainValue(mGraph1->dataCount()-1);
//  double graph2Value = mGraph2->dataMainValue(mGraph2->dataCount()-1);
  mTag1->updatePosition(graph1Value);
//  mTag2->updatePosition(graph2Value);
  mTag1->setText(QString::number(graph1Value, 'f', 3));
//  mTag2->setText(QString::number(graph2Value, 'f', 3));

  mPlot->replot();
}

// 在添加新数据到图表之前，更新队列
void axisTest::addDataAndUpdateQueues(QCPGraph *graph, double value) {
    // 添加新数据到队列
    mGraph1DataQueue.push_back(value); // 假设这是mGraph1的数据
    // 对于mGraph2，你也会做类似的操作

    // 如果队列大小超过100，移除最旧的数据点
    if (mGraph1DataQueue.size() > 100) {
        mGraph1DataQueue.pop_front();
    }
    // 同样，对于mGraph2的队列

    // 添加数据到图表（假设这是在其他地方完成的）
     graph->addData(mGraph1->dataCount(),value);
     mPlot->replot();

}
